import md5 from 'crypto-js/md5';


export default {
        data () {
            return {
                list:[],
                ids:[],
                modal:false,
                filter:{
                  list:[],
                  total:0,
                  page:1,
                  limit:10,
                  name:''

                }
            }
        },
        methods: {
          handleSubmit (name) {
            console.log(this.formValidate)
                this.$refs[name].validate((valid) => {
                    if (valid) {
                      this.formValidate.password = md5(this.formValidate.password).toString();
                      if(this.formValidate._id && this.formValidate._id.length>0){
                        this.$http.put(`http://localhost:3000/${this.module}/data/${this.formValidate._id}`,this.formValidate)
                        .then(res=>{
                            this.$Message.success('提交成功!');
                          this.modal = false;
                          this.getData()
                        })
                      }else{
                        this.$http.post(`http://localhost:3000/${this.module}/data`,this.formValidate)
                        .then(res=>{
                            this.$Message.success('提交成功!');
                          this.modal = false;
                          this.getData()
                        })
                      }
                      

                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            //重置
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            //添加
            onAdd(){
              this.modal = true;
              this.formValidate = {};
              Object.assign(this.$data.formValidate,this.$options.data().formValidate)
            },
            //选择多少条
          onSelectionChange(selection){
            if(selection && selection.length>0){
              var ids = [];
              let selectionLen = selection.length;
              for(let i=0;i<selectionLen;i++){
                ids.push(selection[i]._id)
              }
              this.ids = ids;
            }
          },
          //删除
          onDeletes(){
            

            this.$Modal.confirm({
                    title: '确认删除数据',
                    content: '<p>确认删除该记录吗？</p>',
                    onOk: () => {
                        this.$http.post(`http://localhost:3000/${this.module}/deletes`,{ids:this.ids.toString()})
                .then(res=>{
                  this.$Message.info('删除数据成功');
                  this.getData()
                })
                    },
                    onCancel: () => {
                        
                    }
                });
          },
            modify (index) {
                this.$Modal.info({
                    title: '用户信息',
                    content: `ID：${this.filter.list[index]._id}<br>账号：${this.filter.list[index].name}<br>密码：${this.filter.list[index].password}`
                })
            },
            show (row) {
                this.modal = true;
                Object.assign(this.formValidate,row)

            },
            remove (id) {

              this.$Modal.confirm({
                    title: '确认删除数据',
                    content: '<p>确认删除该记录吗？</p>',
                    onOk: () => {
                        this.$http.delete(`http://localhost:3000/${this.module}/data/${id}`)
                  .then(res=>{
                    this.$Message.info('删除数据成功');
                    this.getData();
                  })
                    },
                    onCancel: () => {
                        
                    }
                });


              
                
            },
            getData(){
              this.$http.post(`http://localhost:3000/${this.module}/list`,this.filter)
            .then(res=>{
                    console.log()
            this.filter.list = res.data.rows;
            this.filter.total = res.data.total;
            this.filter.page = res.data.page;

            })
            },
            onPageSizeChange(pageSize){
              this.filter.limit = pageSize;
              this.getData();
            },
            onChange(page){
              this.filter.page = page;
              this.getData();
            },
            onSearch(){
              this.getData();
            }
            
        
        },
        created(){
          this.getData()
        }
    }